﻿using System.Reflection;

namespace Ma.Bussiness.MaperExtend
{
    public static class MaperHelper
    {
        private static BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

        /// <summary>
        /// 反射 模型转换
        /// </summary>
        /// <typeparam name="T1">变量的类型</typeparam>
        /// <typeparam name="T2">转换的类型</typeparam>
        /// <param name="t1">变量</param>
        /// <returns>使用反射，反射属性字段，对模型进行转换</returns>
        public static T2 Maper<T1, T2>(T1 t1)
        {
            T2 t2 = Activator.CreateInstance<T2>();
            Type type1 = typeof(T1);
            Type type2 = typeof(T2);
            foreach (var item in type2.GetProperties())
            {
                //PropertyType 返回类型 System.Int32
                // Console.WriteLine($"{item.PropertyType}");
                var prop = type1.GetProperty(item.Name);
                if (prop != null)
                {
                    item.SetValue(t2, prop.GetValue(t1));
                }
            }
            return t2;
        }


    }
}
